package com.school.service.impl;

import com.school.entity.Student;
import com.school.entity.User;
import com.school.repository.StudentRepository;
import com.school.service.StudentService;
import com.school.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private UserService userService;

    @Autowired
    private com.school.service.AttendanceService attendanceService;

    @Override
    public List<Student> findAll() {
        return studentRepository.findAll();
    }

    @Override
    public Optional<Student> findById(Long id) {
        return studentRepository.findById(id);
    }

    @Override
    public Optional<Student> findByStudentId(String studentId) {
        return studentRepository.findByStudentId(studentId);
    }

    @Override
    public List<Student> findByClassId(Long classId) {
        return studentRepository.findByClassId(classId);
    }

    @Override
    public List<Student> search(Long classId, String keyword) {
        if (classId != null && classId > 0) {
            if (keyword != null && !keyword.trim().isEmpty()) {
                return studentRepository.findByClassIdAndNameOrStudentIdContaining(classId, keyword);
            } else {
                return studentRepository.findByClassId(classId);
            }
        } else {
            if (keyword != null && !keyword.trim().isEmpty()) {
                return studentRepository.findByNameOrStudentIdContaining(keyword);
            } else {
                return studentRepository.findAll();
            }
        }
    }

    @Override
    public Student save(Student student) {
        return studentRepository.save(student);
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        studentRepository.findById(id).ifPresent(student -> {
            // 1. 删除该学生的考勤记录
            attendanceService.deleteByStudentId(student.getStudentId());

            // 2. 删除学生记录
            studentRepository.deleteById(id);

            // 3. 删除对应的用户账号
            userService.deleteByUsername(student.getStudentId());
        });
    }

    @Override
    @Transactional
    public void deleteByStudentId(String studentId) {
        studentRepository.findByStudentId(studentId).ifPresent(student -> {
            // 1. 删除该学生的考勤记录
            attendanceService.deleteByStudentId(studentId);

            // 2. 删除学生记录
            studentRepository.delete(student);

            // 3. 删除对应的用户账号
            userService.deleteByUsername(studentId);
        });
    }

    @Override
    @Transactional
    public Student createStudentWithUser(Student student) {
        // 1. 保存学生信息
        Student savedStudent = studentRepository.save(student);

        // 2. 创建对应的用户账号（账号和密码都是学号）
        User user = new User();
        user.setUsername(student.getStudentId());
        user.setPassword(student.getStudentId()); // 密码会在UserService中加密
        user.setName(student.getName());
        user.setRole("student");
        user.setClassId(student.getClassId());
        user.setStudentId(student.getStudentId());
        user.setPhone(student.getPhone());

        userService.save(user);

        return savedStudent;
    }
}
